package com.frogs.ucenter.v1.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.util.PageList;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.dao.UserIntegralConsumeRecordDao;
import com.frogs.ucenter.entity.UserIntegralConsumeRecord;
import com.frogs.ucenter.entity.UserIntegralConsumeRecordExt;
import com.frogs.ucenter.v1.request.IntegralConsumeRecordListRequest;
import com.frogs.ucenter.v1.request.UserIntegralConsumeRecordListRequest;
import com.frogs.ucenter.v1.response.PageListResponse;
import com.frogs.ucenter.v1.service.UserIntegralConsumeRecordService;
import com.frogs.ucenter.v1.util.ApiConstants;

/**
 * 
 * @author DLM
 */
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service(ApiConstants.VERSION+"userIntegralConsumeRecordService")
public class UserIntegralConsumeRecordServiceImpl implements UserIntegralConsumeRecordService {
	
	private Logger log = LoggerFactory.getLogger(UserIntegralConsumeRecordServiceImpl.class);
	
	@Resource
	private UserIntegralConsumeRecordDao integralConsumeRecordDao;
	
	/**
	 * 获取用户积分消费列表
	 */
	@Override
	public Response getUserIntegralConsumeRecordList(Request request) throws Exception  {
		PageListResponse<UserIntegralConsumeRecord> response = new PageListResponse<UserIntegralConsumeRecord>();
		try {
			UserIntegralConsumeRecordListRequest integralConsumeRecordListRequest = (UserIntegralConsumeRecordListRequest) request;
			Integer userId = integralConsumeRecordListRequest.getUserId();
			Integer pageNo = integralConsumeRecordListRequest.getPageNum();
			Integer pageSize = integralConsumeRecordListRequest.getNumPerPage();
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("eqUserId", userId);
			params.put("orderByClause", "consume_time desc");
			PageList<UserIntegralConsumeRecord> pageList = integralConsumeRecordDao.selectByMap(params, pageNo, pageSize);
			response.setPageTurn(pageList.getPageTurn());
			response.setList(pageList);
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}

	/**
	 * 获取积分消费列表
	 */
	@Override
	public Response getIntegralConsumeRecordList(Request request) throws Exception  {
		PageListResponse<UserIntegralConsumeRecordExt> response = new PageListResponse<UserIntegralConsumeRecordExt>();
		try {
			IntegralConsumeRecordListRequest integralConsumeRecordListRequest = (IntegralConsumeRecordListRequest) request;
			int pageNo = integralConsumeRecordListRequest.getPageNum();
			int pageSize = integralConsumeRecordListRequest.getNumPerPage();
			boolean doCount = integralConsumeRecordListRequest.isDoCount();
			Map<String, Object> params = new HashMap<String, Object>();
			PageList<UserIntegralConsumeRecordExt> pageList = integralConsumeRecordDao.selectJoinUserByMap(params, pageNo, pageSize,doCount);
			
			response.setPageTurn(pageList.getPageTurn());
			response.setList(pageList);
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}
}